package com.chaoyi.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.chaoyi.model.Dictionary;
import com.chaoyi.model.PortraitPictures;
import com.chaoyi.model.PortraitPicturesSet;
import com.chaoyi.service.DictionaryService;
import com.chaoyi.service.PortraitPicturesService;
import com.chaoyi.util.upload.FileType;
import com.chaoyi.util.upload.UploadRes;

/**
 * 写真视频
 */
@Controller
@MultipartConfig
@RequestMapping("/portrait/pictures")
public class PortraitPicturesController {

	@Resource
	PortraitPicturesService portraitPicturesService;
	@Resource
	DictionaryService dictionaryService;

	/**
	 * 初始化跳转页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mod = new ModelAndView("/portrait/pictures/picturesList");
		return mod;
	}

	/**
	 * 首页展示
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("picturesList")
	@ResponseBody
	public JSONObject picturesList(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		String offset = request.getParameter("offset");
		String limit = request.getParameter("limit");
		String type = request.getParameter("type");
		int count = portraitPicturesService.findCount(type);
		List<PortraitPictures> list = new ArrayList<PortraitPictures>();
		if (count > 0) {
			list = portraitPicturesService.findList(offset, limit, type);
			List<Dictionary> dictionaryList = dictionaryService.findChildrenList(1);
			for (PortraitPictures pic : list) {
				for (Dictionary dic : dictionaryList) {
					if (pic.getPicType() == dic.getValue()) {
						pic.setPicTypeName(dic.getChildName());
					}
				}
			}
		}
		json.put("rows", list);
		json.put("total", count);
		return json;
	}

	/**
	 * 新增首页内容跳转页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("addPictures")
	public ModelAndView addPictures(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mod = new ModelAndView("/portrait/pictures/addPictures");
		return mod;
	}

	/**
	 * 保存新增首页内容
	 */
	@ResponseBody
	@RequestMapping("addPicturesSave")
	public JSONObject addPicturesSave(HttpServletRequest request, HttpServletResponse response,
			PortraitPictures entity) {
		JSONObject json = new JSONObject();
		int number = portraitPicturesService.addPicturesSave(entity);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}

	/**
	 * 修改首页内容
	 */
	@RequestMapping("/alertPictures")
	public ModelAndView alertPictures(HttpServletRequest request, HttpServletResponse response, int id) {
		ModelAndView mod = new ModelAndView("/portrait/pictures/alertPictures");
		PortraitPictures entity = portraitPicturesService.findPicturesOne(id);
		mod.addObject("obj", entity);
		return mod;
	}

	/**
	 * 保存修改的私播信息
	 */
	@ResponseBody
	@RequestMapping("alertPicturesSave")
	public JSONObject alertPicturesSave(HttpServletRequest request, HttpServletResponse response,
			PortraitPictures entity) {
		JSONObject json = new JSONObject();
		int number = portraitPicturesService.alertPicturesSave(entity);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}

	/**
	 * 删除首页内容
	 */
	@ResponseBody
	@RequestMapping("deletePictures")
	public JSONObject deletePictures(HttpServletRequest request, HttpServletResponse response, int id) {
		JSONObject json = new JSONObject();
		int number = portraitPicturesService.deletePictures(id);
		if (number == 1) {
			portraitPicturesService.deletePicturesSetParentId(id);
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}

	/**
	 * 新增首页内容跳转页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("addPicturesSet")
	public ModelAndView addPicturesSet(HttpServletRequest request, HttpServletResponse response, Integer parentId) {
		// 当可选参数“parentId”不存在时，Spring默认将其赋值为null，但由于notIncludeTypeId已定于为基本类型int，所以赋值失败！
		ModelAndView mod = new ModelAndView("/portrait/pictures/addPicturesSet");
		mod.addObject("parentId", parentId);
		return mod;
	}

	/**
	 * 保存新增首页内容
	 */
	@ResponseBody
	@RequestMapping("addPicturesSetSave")
	public JSONObject addPicturesSetSave(HttpServletRequest request, HttpServletResponse response,
			PortraitPicturesSet entity) {
		JSONObject json = new JSONObject();
		if (entity.getPictureUrl().indexOf(",") == -1) {
			portraitPicturesService.addPicturesSetSave(entity);
		} else {
			String[] arrStr = entity.getPictureUrl().split(","); // 批量保存
			for (int i = 0; i < arrStr.length; i++) {
				PortraitPicturesSet pic = new PortraitPicturesSet();
				pic.setParentId(entity.getParentId());
				pic.setPictureUrl(arrStr[i]);
				portraitPicturesService.addPicturesSetSave(pic);
			}
		}
		json.put("missage", "success");
		return json;
	}

	/**
	 * 新增首页封面上传
	 * 
	 * @param response
	 * @param uploadFile
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/uploadClassifyImg")
	@ResponseBody
	public JSONObject uploadClassifyImg(HttpServletResponse response, @RequestParam("file_data") Part uploadFile)
			throws IOException {
		String imgUrl = null;
		try {
			imgUrl = portraitPicturesService.uploadClassifyImg2WSCDN(uploadFile);
		} catch (Exception e) {
			return UploadRes.error("文件上传失败");
		}
		JSONObject result = UploadRes.success()
				.setFileUrl(imgUrl)
				.setFileType(FileType.IMAGE)
				.setDelelteUrl("deletefile")
				.putExtraParam("path", imgUrl).toJSON();
		return result;
	}

	/**
	 * 新增首页内容跳转页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("selectPicturesSetInfo")
	public ModelAndView selectPicturesSetInfo(HttpServletRequest request, HttpServletResponse response,
			Integer parentId) {
		// 当可选参数“parentId”不存在时，Spring默认将其赋值为null，但由于notIncludeTypeId已定于为基本类型int，所以赋值失败！
		ModelAndView mod = new ModelAndView("/portrait/pictures/picturesSetList");
		mod.addObject("parentId", parentId);
		return mod;
	}

	/**
	 * 保存新增首页内容
	 */
	@ResponseBody
	@RequestMapping("picturesSetList")
	public JSONObject picturesSetList(HttpServletRequest request, HttpServletResponse response, Integer parentId) {
		JSONObject json = new JSONObject();
		List<PortraitPicturesSet> list = portraitPicturesService.picturesSetList(parentId);
		json.put("rows", list);
		return json;
	}

	/**
	 * 删除首页内容
	 */
	@ResponseBody
	@RequestMapping("deletePicturesSet")
	public JSONObject deletePicturesSet(HttpServletRequest request, HttpServletResponse response, int id) {
		JSONObject json = new JSONObject();
		int number = portraitPicturesService.deletePicturesSetId(id);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
}
